package com.huxi.lang.object.support;

import java.util.Map;

import com.huxi.lang.object.VIndex;
import com.huxi.lang.object.VMap;
import com.huxi.lang.object.VPath;

public class ComplexPath implements VPath {

	public final String path;
	private final CharSequence[] comps;

	private ComplexPath(String path, CharSequence[] comps, int start) {
		this.path = path;
		this.comps = comps;
	}

	public int compCount() {
		return comps.length;
	}

	public CharSequence comp(int index) {
		return comps[index];
	}

	public boolean isVIndex(int index) {
		return comps[index] instanceof VIndex;
	}

	protected VMap of(Map<String, Object> data) {
		return null;
	}

	protected VMap of(Object data) {
		return null;
	}

	public Object getValue(Map<String, Object> root, Object defaultValue) {
		Object rs = null;
		VMap vmap = of(root);
		int i = 0;
		CharSequence comp = comps[i];
		while (i < comps.length) {
			if (comp instanceof VIndex) {
				rs = vmap.get((VIndex) comp);
			} else {
				rs = vmap.get((String) comp);
			}
			if (rs == null) {
				return defaultValue;
			}
			if (++i < comps.length) {
				comp = comps[i];
				vmap = of(rs);
				continue;
			}
			break;
		}
		return rs;
	}
}
